package org.csu.mypetstore.web.servlets.Ajax;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.util.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class EmailExistServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email = req.getParameter("email");
        AccountService accountService = new AccountService();
        String email1 = accountService.SelectEmailByEmail(email);

        resp.setContentType("text/json");
        PrintWriter out = resp.getWriter();

        Result result = new Result();
        if(email1.equals("Not Exist") && !email.equals("")){
            result.setEmail(email1);
        }else {
            result.setEmail(email);
        }

        String str = JSON.toJSONString(result);
        out.print(str);
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
